<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .active1 {
      background-color: red;

    }

    .active2 {
      background-color: green;

    }

    .active3 {
      background-color: red;

    }

    ul {
      display: flex;
      list-style: none;
    }

    li {
      margin: 15px;
    }
  </style>
</head>

<body>
  <div id="app">
    =={{msg}}==={{activeIndex}}
    <hr />

    <div :class="[flag?active:'','aa',bb]">我是一个class</div>
    <div :class="{'active':false,'bb':true,aa:flag}">我是一个class</div>
    <hr />
    数组写法
    <ul>
      <li @click="activeIndex=item" :class="['aa',item==activeIndex?'active1':'']" v-for="item in  4" :key="item">
        我是第{{item}}个li</li>
    </ul>
    <hr />
    对象写法
    <ul>
      <li @click="activeIndex=item" :class="{active2:activeIndex==item,aa:true}" v-for="item in  4" :key="item">
        我是第{{item}}个li</li>
    </ul>
    <hr />
    对象或者数组抽离写法
    <ul>
      <li @click="activeIndex=item" :class="{active3:activeIndex==item,...objClass}" v-for="item in  4" :key="item">
        我是第{{item}}个li</li>
    </ul>
  </div>
  <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.7.14/vue.min.js"></script>
  <script>
    var vm = new Vue({
      el: "#app",
      data: {
        msg: 'hello  vue',
        active: 'active',
        bb: 'bb',
        flag: true,
        activeIndex: 1,
        objClass: { aa: true }
      },
    })

    // console.log(vm, 2222);
  </script>
</body>

</html>